<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>关闭游标，使语句能再次被执行。</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.bindvalue.html">PDOStatement::bindValue</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.columncount.html">PDOStatement::columnCount</a></div>
 <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdostatement.closecursor" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::closeCursor</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.9.0)</p><p class="refpurpose"><span class="refname">PDOStatement::closeCursor</span> &mdash; <span class="dc-title">
   关闭游标，使语句能再次被执行。
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.closecursor-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>PDOStatement::closeCursor</strong></span>
    ( <span class="methodparam">void</span>
   )</div>


  <p class="para rdfs-comment">
   <span class="function"><strong>PDOStatement::closeCursor()</strong></span> 释放到数据库服务的连接，以便发出其他 SQL 语句，但使语句处于一个可以被再次执行的状态。
  </p>
  <p class="para">
   当上一个执行的 PDOStatement 对象仍有未取行时，此方法对那些不支持再执行一个 PDOStatement 对象的数据库驱动非常有用。 如果数据库驱动受此限制，则可能出现失序错误的问题。
  </p>
  <p class="para">
   <span class="function"><strong>PDOStatement::closeCursor()</strong></span> 要么是一个可选驱动的特有方法（效率最高）来实现，要么是在没有驱动特定的功能时作为一般的PDO 备用来实现。一般的备用语义上与下面的 PHP 代码相同：
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nextRowset</span><span style="color: #007700">())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.closecursor-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.closecursor-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-913">
    <p><strong>Example #1 一个 <span class="function"><strong>PDOStatement::closeCursor()</strong></span> 的例子</strong></p>
    <div class="example-contents"><p>
    在下面例子中，<var class="varname"><var class="varname">$stmt</var></var> PDOStatement 对象返回多行，但应用程序只取第一行，让  PDOStatement 对象处于一个有未取行的状态。为确保应用程序对所有数据库驱动都能正常运行，在执行 <var class="varname"><var class="varname">$otherStmt</var></var> PDOStatement 对象前，<var class="varname"><var class="varname">$stmt</var></var> 调用一次 PDOStatement::closeCursor() 。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;创建一个&nbsp;PDOStatement&nbsp;对象&nbsp;*/<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;foo&nbsp;FROM&nbsp;bar'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;创建第二个&nbsp;PDOStatement&nbsp;对象&nbsp;*/<br /></span><span style="color: #0000BB">$otherStmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;foobaz&nbsp;FROM&nbsp;foobar'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;执行第一条语句&nbsp;*/<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;&nbsp;从结果集中只取出第一行&nbsp;*/<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;The&nbsp;following&nbsp;call&nbsp;to&nbsp;closeCursor()&nbsp;may&nbsp;be&nbsp;required&nbsp;by&nbsp;some&nbsp;drivers&nbsp;*/<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">closeCursor</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;&nbsp;现在可以执行第二条语句了&nbsp;*/<br /></span><span style="color: #0000BB">$otherStmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.closecursor-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - 执行一条预处理语句</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.bindvalue.html">PDOStatement::bindValue</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.columncount.html">PDOStatement::columnCount</a></div>
 <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
